<script setup lang="ts">
import { Notifications, Person } from '@vicons/ionicons5'
import { Labels, NavProp, ChatGroup, UserInfo } from '@c/common/BoxTypes.ts'
import { Ref, ref } from 'vue'
import { useTestStore } from '@/store'
import SignIn from '@/views/user/SignIn.vue'

const {
  BoxTheme: {
    currentTheme: { fontColor, lightColor }
  }
} = useTestStore()
const toCommunityPage = () => {
  console.log('toCommunityPage')
}
const userPopShow = ref(false)
// props
const props = defineProps<{ navProp: NavProp }>()
const { title, titleIcon, labels } = props.navProp
// emit
const emit = defineEmits<{
  (e: 'clickLabelHandle', label: Labels): void
}>()
const clickTitleHandle = (label: Labels) => {
  emit('clickLabelHandle', label)
}
// init
const isSignIn: Ref<boolean> = ref(true)
const signInBoxModal: Ref<boolean> = ref(false)
const chatGroup: Ref<ChatGroup[]> = ref([])
const messageGroupUnread = computed(() => {
  return chatGroup.value?.reduce((pre, cur) => {
    return pre + cur.unread
  }, 0)
})
const userInfo: Ref<UserInfo> = ref({
  name: '...',
  price: 0,
  boxLevel: {
    level: 'BOX.0',
    currentLevelEXP: 4,
    currentLevelAllExp: 8
  },
  features: [],
  fast: []
})
</script>

<template>
  <n-flex justify="space-between" :wrap="false">
    <div class="logo-titles">
      <n-icon @click="toCommunityPage()" :component="titleIcon" size="32" />
      <span @click="toCommunityPage()" class="name">{{ title }}</span>
      <div style="height: 100%; padding: 16px 0">
        <ul class="titles">
          <li v-for="(label, index) in labels" :key="index" @click="clickTitleHandle(label)">
            <span>{{ label.name }}</span>
          </li>
        </ul>
      </div>
    </div>
    <!--    <div class="search">
    <n-input :clearable="true" placeholder="搜索" @change="search" style="min-width: 100%">
      <template #suffix>
        <n-icon :component="Search" />
      </template>
    </n-input>
  </div>-->
    <div class="other">
      <div class="message" style="cursor: pointer">
        <n-badge v-if="isSignIn" :value="messageGroupUnread" :max="99" :offset="[4, -4]" style="--n-font-size: 8px">
          <n-icon size="24">
            <Notifications />
          </n-icon>
        </n-badge>
        <div v-if="isSignIn" class="message-pop">
          <div class="message-pop-item" v-for="group in chatGroup" :key="group.router">
            {{ group.name }}
            <n-badge :value="group.unread" :max="99" style="--n-font-size: 8px"></n-badge>
          </div>
        </div>
      </div>
      <div class="user">
        <n-avatar v-if="isSignIn" @click="userPopShow = !userPopShow" size="large" src="https://07akioni.oss-cn-beijing.aiyuncs.com/07akioni.jpeg">{{
          userInfo.name
        }}</n-avatar>
        <div v-if="isSignIn && userPopShow" class="user-pop">
          <div class="userInfo">
            <n-avatar size="large" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg">{{ userInfo.name }}</n-avatar>
            <div class="name">
              <span>FancyBox</span>
              <span>
                余额:
                {{
                  userInfo.price.toLocaleString('zh-CN', {
                    style: 'currency',
                    currency: 'CNY'
                  })
                }}
                >
              </span>
            </div>
          </div>
          <div class="grade">
            <div class="grade-1">
              <span
                >盒友等级: <span class="gradeNum">{{ userInfo.boxLevel.level }}</span></span
              >
              <span>{{ userInfo.boxLevel.currentLevelEXP }} / {{ userInfo.boxLevel.currentLevelAllExp }} ></span>
            </div>
            <n-progress
              :style="{ '--n-fill-color': fontColor }"
              type="line"
              :show-indicator="false"
              :percentage="(userInfo.boxLevel.currentLevelEXP / userInfo.boxLevel.currentLevelAllExp) * 100"
            />
          </div>
          <n-divider></n-divider>
          <div class="fast">
            <div>
              <span>1</span>
              <span>关注</span>
            </div>
            <div>
              <span>10</span>
              <span>赞过</span>
            </div>
            <div>
              <span>11</span>
              <span>收藏</span>
            </div>
            <div>
              <span>1</span>
              <span>关注</span>
            </div>
            <div>
              <span>10</span>
              <span>赞过</span>
            </div>
            <div>
              <span>11</span>
              <span>收藏</span>
            </div>
          </div>
          <n-divider></n-divider>
          <div class="features">
            <div v-for="(feature, index) in userInfo.features" :key="index">
              <n-icon size="24" color="#4098fc">
                <Person />
              </n-icon>
              <span>{{ feature.name }}</span>
            </div>
          </div>
          <n-divider></n-divider>
          <div class="operate">
            <span>我的设置</span>
            <span>退出登录</span>
          </div>
        </div>
      </div>
      <n-button v-if="!isSignIn" type="info" strong secondary @click="signInBoxModal = !signInBoxModal"> 登录/注册 </n-button>
    </div>
  </n-flex>
  <div class="n-flex-Height"></div>
  <SignIn v-model:visible="signInBoxModal" />
</template>

<style scoped lang="scss">
$minWidth: 320px;
$navHeight: 64px;
$titleWidth: 64px;
$logoWidth: 80px;

.n-flex-Height {
  height: $navHeight;
  border-bottom: 1px solid red;
}

.n-flex {
  position: absolute;
  background-color: white;
  z-index: 3002;
  width: 100%;
  padding: 0 128px;
  align-items: center;
  min-width: $minWidth;
  height: $navHeight;

  .logo-titles {
    display: flex;
    align-items: center;

    //width: calc(v-bind('labels.length') * $titleWidth + $logoWidth);
    height: 100%;

    .n-icon {
      color: v-bind('fontColor');
    }

    .name {
      font-size: 24px;
      font-weight: 800;
      white-space: nowrap;
      padding-top: 4px;
    }

    > span,
    > .n-icon {
      cursor: pointer;
    }

    .titles {
      overflow: hidden;
      white-space: nowrap; //处理块元素中的空白符和换行符的，这个属性保证图片不换行
      font-size: 16px;

      //width: calc(v-bind('labels.length') * $titleWidth);
      background-color: white;
      font-weight: 800;
      color: #565657;

      li:hover {
        color: v-bind('fontColor');
      }

      li {
        cursor: pointer;

        //width: 48px;
        float: left; //向左排列
        margin: 0 8px;
        padding: 4px 8px;
      }
    }
  }

  /* .search {
    width: 360px;
  } */
  .other {
    display: flex;
    align-items: center;
    width: 96px;
    height: 100%;
    justify-content: space-between;

    .message {
      cursor: pointer;
      height: 100%;
      display: flex;
      align-items: center;

      .n-icon {
        color: $dark;
      }

      .message-pop {
        position: absolute;
        top: 60px;
        right: 186px;
        background-color: white;
        width: 160px;
        box-shadow: 0 0 8px 4px #e7e7e7;
        border-radius: 2%;
        display: none;
        padding: 8px;

        .message-pop-item {
          font-size: 16px;
          padding: 4px 8px;
          height: 40px;
          line-height: 32px;
          border-radius: 2%;
          display: flex;
          justify-content: space-between;
          align-items: center;
        }

        .message-pop-item:hover {
          background-color: $darkLight;
        }
      }

      .message-pop:hover {
        display: block;
      }
    }

    .user {
      cursor: pointer;
      height: 100%;
      display: flex;
      align-items: center;

      .user-pop {
        $userPop: 300px;
        $userPopPadding: 24px;
        $contentWidth: $userPop - $userPopPadding * 2;

        position: absolute;
        top: 60px;
        right: 124px;
        background-color: white;
        width: $userPop;
        box-shadow: 0 0 8px 4px #e7e7e7;
        border-radius: 2%;
        padding: $userPopPadding $userPopPadding;
        z-index: 2001;
        cursor: default;

        .userInfo {
          margin-bottom: 10px;
          cursor: pointer;
          display: flex;

          .name {
            display: flex;
            flex-direction: column;
            margin-left: 16px;

            span:first-child {
              font-weight: 800;
            }
          }
        }

        .grade {
          cursor: pointer;
          color: v-bind('fontColor');
          background-color: v-bind('lightColor');
          border-radius: 4px;
          padding: 8px;

          .grade-1 {
            display: flex;
            justify-content: space-between;

            .gradeNum {
              font-weight: 800;
            }
          }
        }

        .fast {
          display: flex;
          justify-content: space-around;
          width: $contentWidth;
          flex-wrap: wrap;

          div {
            $divBox: 56px;

            cursor: pointer;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin: 0 calc(($contentWidth - $divBox * 3) / 6);
            height: $divBox;
            width: $divBox;
            justify-content: space-evenly;
            border-radius: 4px;

            span:last-child {
              color: $darkHover;
            }

            span:first-child {
              color: $blueFont;
            }
          }

          div:nth-child(-n + 3) {
            span:first-child {
              color: $blueFont;
            }

            span:last-child {
              color: $darkHover;
            }
          }

          div:nth-child(-n + 3):hover {
            background-color: $blueLight;
          }

          div:nth-child(n + 4) {
            span:first-child {
              color: $redFont;
            }

            span:last-child {
              color: $darkHover;
            }
          }

          div:nth-child(n + 4):hover {
            background-color: $redLight;
          }
        }

        .features {
          width: $contentWidth;
          display: flex;
          flex-wrap: wrap;
          justify-content: flex-start;

          div {
            .n-icon {
              color: $blueFont;
            }

            $featureMargin: 4px;
            $featurePadding: 4px;

            cursor: pointer;
            width: calc($contentWidth / 2 - $featurePadding * 2);
            text-align: center;
            margin: $featureMargin;
            padding: $featurePadding $featurePadding;
            box-sizing: border-box;
            border-radius: 4px;
            display: flex;
            align-items: flex-end;
            justify-content: space-around;

            //background-color: $darkLight;
          }

          div:hover {
            background-color: $blueLight;
          }
        }

        .operate {
          display: flex;
          justify-content: space-around;

          span {
            cursor: pointer;
          }

          span:hover {
            color: v-bind('fontColor');
          }
        }
      }
    }

    .message:hover .message-pop {
      display: block;
    }

    .message:hover .n-icon {
      color: v-bind('fontColor');
    }

    .n-button {
      height: 34px;
      font-size: 12px;
    }
  }
}

// media width < 1024px

/* @media screen and (max-width: 1024px) and (min-width: 768px) {
  .n-flex {
    padding: 0 16px;
    .other {
      .user > .user-pop {
        right: 15px;
      }
      .message > .message-pop {
        right: 72px;
      }
    }
  }
} */
// media width < 768px
@media screen and (width <= 1024px) {
  .n-flex {
    padding: 0 16px;

    .logo-titles {
      width: calc($logoWidth + $titleWidth + 16px);

      .titles {
        width: 80px;
        overflow-y: visible;
        text-align: center;

        li {
          margin: 0;
          width: 80px;
          float: none;
        }

        li:not(:first-child) {
          margin: 8px 0;
          display: none;
        }

        li:first-child > span::after {
          content: '';
          width: 0;
          height: 0;
          border-top: 4px solid v-bind('fontColor');
          border-left: 4px solid transparent;
          border-right: 4px solid transparent;
          border-bottom: 0;
          position: relative;
          top: 12px;
          left: 4px;
          overflow: hidden;
        }

        li:first-child > span::before {
          content: '';
          width: 0;
          height: 0;
          border-left: 4px solid transparent;
          border-right: 4px solid transparent;
          border-bottom: 4px solid rgb(255 255 255 / 0%);
          position: relative;
          top: -12px;
          left: -4px;
        }
      }

      .titles:hover > li {
        display: block;
      }

      .titles:hover > li:first-child > span::after {
        content: '';
        width: 0;
        height: 0;
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
        border-bottom: 4px solid v-bind('fontColor');
        position: relative;
        top: -12px;
        left: 4px;

        //border-bottom: 4px solid #00ee26;
      }
    }

    .search {
      width: 240px;
    }

    .other {
      .user > .user-pop {
        right: 15px;
      }

      .message > .message-pop {
        right: 72px;
      }
    }
  }
}

/* 禁用水平滚动条 */
::-webkit-scrollbar {
  height: 0;
}

/* 禁用垂直滚动条 */
::-webkit-scrollbar {
  width: 0;
}
</style>
